﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace MyFinanceManager.Objects
{
    public class CatItem : List<CatItem>
    {
        #region Property
        /// <summary>
        /// Gets the records.
        /// </summary>
        /// <value>The records.</value>
        public ObservableCollection<PostingRecord> Records { get; private set; }
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }
        /// <summary>
        /// Gets a value indicating whether this instance has children.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has children; otherwise, <c>false</c>.
        /// </value>
        public bool HasChildren
        {
            get { return this.Count > 0; }
        }
        /// <summary>
        /// Gets the saldo.
        /// </summary>
        /// <value>The saldo.</value>
        public double Saldo
        {
            get
            {
                double value = 0;
                foreach (var item in this.Records)
                {
                    value += item.Amount;
                }
                return value;
            }
        }
        /// <summary>
        /// Gets the saldo (including all children).
        /// </summary>
        /// <value>The saldo (including all children).</value>
        public double SaldoKumuliert
        {
            get
            {
                double value = 0;
                if (this.HasChildren)
                {
                    foreach (var item in this)
                    {
                        value += item.SaldoKumuliert;
                    }
                }

                value += this.Saldo;
                return value;
            }
        }

        #region Just4View
        /// <summary>
        /// Gets a combination of the name and the saldo e.g. "PC (10,00 €)"
        /// </summary>
        /// <value>The name and saldo kum.</value>
        public string NameAndSaldoKum
        {
            get
            {
                return String.Format("{0} ({1:C})", this.Name, this.SaldoKumuliert);
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="CatItem"/> class.
        /// </summary>
        public CatItem()
        {
            this.Records = new ObservableCollection<PostingRecord>();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds a new item with a specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        internal CatItem Add(string name)
        {
            this.Add(new CatItem { Name = name });
            return this.Last<CatItem>();
        }
        /// <summary>
        /// Finds the specified name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        internal CatItem Find(string name)
        {
            CatItem ci = Find(name, this);
            return ci;
        }
        /// <summary>
        /// Finds the specified name in a defined CatItem
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="dictionary">The dictionary.</param>
        /// <returns></returns>
        private static CatItem Find(string name, CatItem dictionary)
        {
            CatItem found = null;
            foreach (var item in dictionary)
            {
                if (item.HasChildren)
                {
                    found = Find(name, item);
                    if (found != null)
                        break;
                }
                if (String.Compare(item.Name, name, true) == 0)
                {
                    found = item;
                    break;
                }
            }
            return found;
        }
        #region Methods (overrides)
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return String.Format("{0} {1:N0}", this.Name, this.Count);
        }
        #endregion
        #endregion
    }
}
